package com.project.imageinterference.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.imageinterference.dto.request.DiseaseIdentifyPageReq;
import com.project.imageinterference.dto.request.DiseaseIdentifyReq;
import com.project.imageinterference.dto.request.DiseaseIdentifyUpdateReq;
import com.project.imageinterference.dto.response.DiseaseIdentifyPageRes;
import com.project.imageinterference.dto.response.DiseaseIdentifyRes;
import com.project.imageinterference.entity.DiseaseIdentify;
import com.project.imageinterference.entity.SysUser;
import com.project.imageinterference.exception.BusinessException;
import com.project.imageinterference.mapper.DiseaseIdentifyMapper;
import com.project.imageinterference.service.DiseaseIdentifyService;
import com.project.imageinterference.vo.Paging;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 疾病识别表 服务实现类
 * </p>
 *
 * @author haochuanwan
 * @since 2025-02-22
 */
@Service
public class DiseaseIdentifyServiceImpl extends ServiceImpl<DiseaseIdentifyMapper, DiseaseIdentify> implements DiseaseIdentifyService {

    @Resource
    private DiseaseIdentifyMapper diseaseIdentifyMapper;

    @Override
    public void saveDiseaseIdentify(DiseaseIdentifyReq diseaseIdentifyReq, SysUser sysUserPo) {
        // 空值校验
        if(StringUtils.isEmpty(diseaseIdentifyReq.getDiseaseOverview())){
            throw new BusinessException("疾病概况不能为空");
        }
        if(StringUtils.isEmpty(diseaseIdentifyReq.getImageBase64())){
            throw new BusinessException("图片二进制不能为空");
        }
        DiseaseIdentify diseaseIdentify = new DiseaseIdentify();
        BeanUtil.copyProperties(diseaseIdentifyReq, diseaseIdentify);
        diseaseIdentify.setCreateUserId(sysUserPo.getId());
        LocalDateTime now = LocalDateTime.now();
        diseaseIdentify.setCreateTime(now);
        diseaseIdentify.setUpdateTime(now);
        diseaseIdentifyMapper.insert(diseaseIdentify);
    }

    @Override
    public DiseaseIdentifyRes getDiseaseIdentifyInfo(Integer id) {
        if(ObjectUtils.isEmpty(id)){
            throw new BusinessException("识别id不能为空");
        }
        DiseaseIdentify diseaseIdentify = diseaseIdentifyMapper.selectById(id);
        if(ObjectUtils.isEmpty(diseaseIdentify)){
            throw new BusinessException("识别记录查询为空");
        }
        DiseaseIdentifyRes diseaseIdentifyRes = new DiseaseIdentifyRes();
        BeanUtil.copyProperties(diseaseIdentify, diseaseIdentifyRes);
        return diseaseIdentifyRes;
    }

    @Override
    public Paging<DiseaseIdentifyPageRes> queryDiseaseIdentifyPage(DiseaseIdentifyPageReq req) {
        //TODO 后续需要集成用户，每个人只能看到自己的识别记录，admin用户可以看到所有人的识别记录
        String userId = "1";
        LambdaQueryWrapper<DiseaseIdentify> queryWrapper = new LambdaQueryWrapper<>();
        if(!Objects.isNull(req)){
            queryWrapper.like(!StringUtils.isEmpty(req.getKeyword()), DiseaseIdentify::getDiseaseOverview, req.getKeyword());
            queryWrapper.ge(!StringUtils.isEmpty(req.getStartTime()), DiseaseIdentify::getCreateTime, req.getStartTime());
            queryWrapper.le(!StringUtils.isEmpty(req.getEndTime()), DiseaseIdentify::getCreateTime, req.getEndTime());
            if(!"1".equals(userId)){
                queryWrapper.eq(!StringUtils.isEmpty(userId), DiseaseIdentify::getCreateUserId, userId);
            }
            queryWrapper.eq(DiseaseIdentify::getIsDeleted, 0);
            queryWrapper.orderByDesc(DiseaseIdentify::getCreateTime);
        }
        Page<DiseaseIdentify> page = new Page<>(req.getPageNum(), req.getPageSize());
        page = diseaseIdentifyMapper.selectPage(page, queryWrapper);
        Paging<DiseaseIdentifyPageRes> dataPage = new Paging<>();
        dataPage.setData(page.getRecords().stream().map(e->{
            DiseaseIdentifyPageRes dto = new DiseaseIdentifyPageRes();
            BeanUtils.copyProperties(e, dto);
            return dto;
        }).collect(Collectors.toList()));
        dataPage.setTotal(page.getTotal());
        return dataPage;
    }

    @Override
    public void updateDiseaseIdentify(DiseaseIdentifyUpdateReq diseaseIdentifyUpdateReq) {
        // 更新识别结果
        LambdaQueryWrapper<DiseaseIdentify> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DiseaseIdentify::getId, diseaseIdentifyUpdateReq.getId());
        queryWrapper.eq(DiseaseIdentify::getIsDeleted, 0);
        List<DiseaseIdentify> diseaseIdentifies = diseaseIdentifyMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(diseaseIdentifies)){
            throw new BusinessException("识别记录查询为空");
        }
        DiseaseIdentify diseaseIdentify = diseaseIdentifies.get(0);
        BeanUtils.copyProperties(diseaseIdentifyUpdateReq,diseaseIdentify);
        diseaseIdentify.setUpdateTime(LocalDateTime.now());
        diseaseIdentifyMapper.updateById(diseaseIdentify);
    }

    @Override
    public void deleteDiseaseIdentify(Integer id) {
        if(ObjectUtils.isEmpty(id)){
            throw new BusinessException("识别id不能为空");
        }
        LambdaQueryWrapper<DiseaseIdentify> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DiseaseIdentify::getIsDeleted, 0);
        queryWrapper.eq(DiseaseIdentify::getId,id);
        DiseaseIdentify diseaseIdentify = diseaseIdentifyMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(diseaseIdentify)){
            throw new BusinessException("查询为空");
        }
        // 删除识别结果
        diseaseIdentifyMapper.delete(queryWrapper);
    }
}
